package schoolWork;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TemperatureConverter extends JFrame implements ActionListener {
    private JTextField fahrenheitField, celsiusField;

    public TemperatureConverter() {
        setTitle("温度转换");
        setLayout(new FlowLayout());

        fahrenheitField = new JTextField(10);
        celsiusField = new JTextField(10);
        celsiusField.setEditable(false);

        JButton convertButton = new JButton("转换");
        convertButton.addActionListener(this);

        add(new JLabel("华氏温度:"));
        add(fahrenheitField);
        add(new JLabel("摄氏温度:"));
        add(celsiusField);
        add(convertButton);

        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        double fahrenheit = Double.parseDouble(fahrenheitField.getText());
        double celsius = (fahrenheit - 32) * 5 / 9;
        celsiusField.setText(String.format("%.2f", celsius));
    }

    public static void main(String[] args) {
        new TemperatureConverter();
    }
}
